package java01_type;

import cn.hutool.core.util.ObjectUtil;
import java01_type.common.User;
import org.junit.jupiter.api.Test;

import java.util.Objects;

public class Test01_Number {

    /**
     * Integer缓存范围：-128~127
     * 所以Integer最好用equals()比较
     */
    @Test
    void test01_Integer() {
        Integer a = 127;
        Integer b = 127;
        System.out.println(a == b);

        a = -128;
        b = -128;
        System.out.println(a == b);

        a = 128;
        b = 128;
        System.out.println(a == b);
        System.out.println(a.equals(b));
    }

    /**
     * Bean中
     * 包装器字段Integer默认为NULL
     * 基本类型int默认为0
     * 基本类型double默认为0.0
     * 基本类型boolean默认也为null
     */
    @Test
    void test02_Bean_Integer() {
        User user = new User();
        System.out.println(user);
    }

    /**
     * 判断两个Integer是否相等，可以用Objects.equals()
     */
    @Test
    void testNullEquals() {
        Integer a = null;
        Integer b = null;

        System.out.println("a == b " + (a == b));

        boolean compare = Objects.equals(a, b);
        System.out.println(compare);

        boolean compare2 = ObjectUtil.compare(a, b) == 0;
        System.out.println(compare2);

        boolean compare3 = ObjectUtil.equals(a, b);
        System.out.println(compare3);
    }

    /**
     * Long在-128 ~ 127 是有缓存的
     * 所以比较相等要用equals()
     */
    @Test
    void testLong () {
        Long a = 128L;
        Long b = Long.valueOf(128);
        System.out.println(a == b); //false
        System.out.println(a.equals(b)); //true

        Long aa = 127L;
        Long bb = Long.valueOf(127);
        System.out.println(aa == bb); //true
    }

    /**
     * 浮点数不能精确表示数字，不适合做比较
     */
    @Test
    void testDouble() {
        double a = 9.999999999999999999;
        double b = 9.9999999999999999999;
        double c = 10.00;
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        System.out.println(a == b);
        System.out.println(a == c);
    }
}
